package com.kai.steering.data.service.service.impl;

import com.kai.steering.common.vo.Result;
import com.kai.steering.common.web.vo.ReturnCodeExt;
import com.kai.steering.data.service.entity.domainObject.ClassificationDO;
import com.kai.steering.data.service.entity.dto.ClassificationQueryDTO;
import com.kai.steering.data.service.entity.po.Classification;
import com.kai.steering.data.service.entity.vo.ClassificationVO;
import com.kai.steering.data.service.mapper.ClassificationMapper;
import com.kai.steering.data.service.service.ClassificationService;
import com.kai.steering.common.web.service.impl.BaseServiceImpl;
import com.kai.steering.common.vo.Page;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;

/**
 * <pre>
 * 题目分类 服务实现类
 * </pre>
 *
 * @author kai
 * @since 2022-02-23
 */
@Slf4j
@Service
public class ClassificationServiceImpl extends BaseServiceImpl<ClassificationMapper, Classification> implements ClassificationService {

    @Autowired
    private ClassificationMapper classificationMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveClassification(ClassificationDO classificationDO) {
        /**
         * 参数验证
         */
        ClassificationDO old = this.getClassificationByName(classificationDO.getClassificationName());
        if (old!=null|| StringUtils.isNotEmpty(old.getClassificationId())) {
            throw ReturnCodeExt.PARAMETER_CHECK.getException("对象名字[" + classificationDO.getClassificationName() + "]不存在");
        }
        /**
         * 构建对象
         */
        Classification classification = new Classification(classificationDO);
        return super.save(classification);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateClassification(Serializable id, ClassificationDO classificationDO) {
        /**
         * 参数验证
         */
        ClassificationDO oldClassification = getClassificationById(id);
        //id
        if (oldClassification==null||StringUtils.isEmpty(oldClassification.getClassificationId())){
            throw ReturnCodeExt.PARAMETER_CHECK.getException("对象id[" + oldClassification.getClassificationId() + "]不存在");
        }
        /**
         * 构建对象
         */
        Classification classification = new Classification(classificationDO);
        classification.setCreationDatetime(oldClassification.getCreationDatetime());
        return super.updateById(classification);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteClassification(Serializable id) {
        /**
         * 参数验证
         */
        ClassificationDO oldClassification = getClassificationById(id);
        //id
        if (oldClassification==null||StringUtils.isEmpty(oldClassification.getClassificationId())){
            throw ReturnCodeExt.PARAMETER_CHECK.getException("对象id[" + oldClassification.getClassificationId() + "]不存在");
        }
        super.removeById(id);
        return true;
    }

    @Override
    public ClassificationDO getClassificationById(Serializable id) {

        Classification classification = super.getById(id);
        ClassificationDO classificationDO = new ClassificationDO(classification);
        return classificationDO;
    }

    @Override
    public List<ClassificationVO> getAllClassification() {
        List<Classification> classificationlist = classificationMapper.selectList(new QueryWrapper<Classification>());
        List<ClassificationVO> result = classificationlist.stream()
                .map(classification -> {
                    ClassificationVO classificationVo = new ClassificationVO();
                    BeanUtil.copyProperties(classification, classificationVo);
                    return classificationVo;
                })
                .collect(Collectors.toList());
        return result;
    }

    @Override
    public Page<ClassificationVO> getClassificationPageList(ClassificationQueryDTO classificationQueryDTO) {
        com.baomidou.mybatisplus.extension.plugins.pagination.Page page = setPageParam(
                classificationQueryDTO, OrderItem.desc("creation_datetime")
        );

        QueryWrapper<Classification> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("usable",0);
        if ((StringUtils.isNotEmpty(classificationQueryDTO.getClassificationId()))){
            queryWrapper.eq("classification_id",classificationQueryDTO.getClassificationId());
        }
        if ((StringUtils.isNotEmpty(classificationQueryDTO.getContent()))){
            queryWrapper.like("content",classificationQueryDTO.getContent());
        }
        if ((StringUtils.isNotEmpty(classificationQueryDTO.getFounder()))){
            queryWrapper.like("founder",classificationQueryDTO.getFounder());
        }




        //开始时间 创建
        if (classificationQueryDTO.getCreationDateOrigin()==null){
            queryWrapper.ge("creation_datetime", classificationQueryDTO.getCreationDateOrigin());
        }
        //结束时间 创建
        if (classificationQueryDTO.getCreationDateTerminus()==null){
            queryWrapper.le("creation_datetime",classificationQueryDTO.getCreationDateTerminus());
        }

        //组合条件
        IPage<Classification> entityPage = classificationMapper.selectPage(page, queryWrapper);
        List<ClassificationVO> result = entityPage.getRecords().stream()
                .map(classification -> {
                    ClassificationVO classificationVo = new ClassificationVO();
                    BeanUtil.copyProperties(classification, classificationVo);
                    return classificationVo;
                })
                .collect(Collectors.toList());

        return Page.<ClassificationVO>builder()
                .currPage(classificationQueryDTO.getPageNo())
                .pageSize(classificationQueryDTO.getPageSize())
                .totalCount((int) entityPage.getTotal())
                .list(result)
                .build();
    }


    @Override
    public boolean isExists(String key, String val, Serializable id) {
        QueryWrapper<Classification> qw = new QueryWrapper<>();
        qw.eq(key, val);
        if (id != null) {
            qw.ne("id", id);
        }
        int num = this.count(qw);
        return num > 0;
    }

    @Override
    public ClassificationDO getClassificationByName(String classificationName) {
        Classification classification= classificationMapper.getClassificationByName(classificationName);
        ClassificationDO classificationDO = new ClassificationDO(classification);
        return classificationDO;
    }

}

